package com.beauty.dressing.module.common.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.beauty.dressing.R;
import com.beauty.dressing.common.bean.Post;
import com.beauty.dressing.common.utils.Tools;
import com.framework.common.base.IBaseActivity;
import com.framework.common.base.IBaseFragmentActivity;
import com.framework.common.utils.ICommonUtil;
import com.framework.common.utils.ILog;

/**
 * @author bond
 * @createtime 2016年03月09日 13:48
 * @company 9zhitx.com
 * @description 点赞区域
 */

public class PraiseArea extends FrameLayout implements View.OnClickListener {
    private final String TAG = getClass().getSimpleName();

    protected Context context;

//    private PariseAnimView mPariseAnimView;
    //private Animation praiseAnimation;
    //private TextView praiseTxt;
    protected TextView numberTxt;
    protected ImageView imageView;

    /**
     * 1.等于null，回调点赞区域的点击事件
     * 2.不等于null，点赞区域的点击事件‘onPraiseAreaClick’不会被回调，PraiseArea内部处理
     */
    private Param param;

    /**
     * 点赞成功之后的动画listener
     */
    private Listener listener;

    public PraiseArea(Context context) {
        super(context);
        init(context, null);
    }

    public PraiseArea(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public PraiseArea(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    protected void init(Context context, AttributeSet attrs) {
        this.context = context;
        addView(LayoutInflater.from(context).inflate(R.layout.area_praise, this, false));
        numberTxt = (TextView) findViewById(R.id.num_txt);
        numberTxt.setText("");
        //praiseTxt = (TextView) findViewById(R.id.zan_anim_tv);
        imageView = (ImageView) findViewById(R.id.img);
        imageView.setImageResource(defaultImageSrc());
        setOnClickListener(this);
    }


    public int defaultImageSrc() {
        return R.drawable.zan;
    }


    private boolean hasPraised; //是否已点赞
    public void setPraised(boolean isPraised) {
        hasPraised = isPraised;
        imageView.setImageResource(isPraised ? R.drawable.zaned : R.drawable.zan);
        numberTxt.setTextColor(isPraised ? Color.parseColor("#e73850") : Color.parseColor("#999999"));
    }

    public void setPraiseListener(Listener listener) {
        this.listener = listener;
    }

    public void setParam(Param param) {
        //praiseTxt.setVisibility(View.INVISIBLE);
        this.param = param;
    }

    public void setPraiseNum(int num) {
        if (0 == num) {
            numberTxt.setText("点赞");
        } else {
            numberTxt.setText("赞"+String.valueOf(num));
        }
    }

    /**
     * 点赞成功
     */
    public void onPraiseSuccess() {
        setPraised(true);
//        if (mPariseAnimView != null && mPariseAnimView.isShowing()) {
//            mPariseAnimView.dismiss();
//        }
//        mPariseAnimView = new PariseAnimView(context);
//        mPariseAnimView.show(imageView, PariseAnimView.TYPE_POSTS_REPLY_PARISE);
    }

    @Override
    public void onClick(View v) {
        if (!Tools.checkNetWorkAndToast()) {
            return;
        }
        if (ICommonUtil.isFastDoubleClick(v.getId())) {
            return;
        }
        if (hasPraised){
            return;
        }
        if (null == param) {
            if (null != listener) {
                listener.onPraiseAreaClick();
            }
        } else {
            if (!param.isPraiseEnable) {
                return;
            }
            // 调用点赞接口
        }
    }


    public void showToast(int resId) {
        if (null != context) {
            showToast(context.getString(resId));
        }
    }

    public void showToast(String title){
        if (context != null ){
            if (context instanceof IBaseActivity){
                ((IBaseActivity)context).showToast(title);
            }
            else if (context instanceof IBaseFragmentActivity){
                ((IBaseFragmentActivity)context).showToast(title);
            }
        }
    }


    /**
     * 内部处理点赞需要的参数
     */
    public static class Param {
        public int moudleId;
        public long businessId;
        public boolean isPraiseEnable = true;//默认需要时true
        public Post posts; // 帖子点赞使用

        public Param(int moudleId, long businessId, boolean isPraiseEnable) {
            this.moudleId = moudleId;
            this.businessId = businessId;
            this.isPraiseEnable = isPraiseEnable;
        }

        public Param(int moudleId, long businessId) {
            this(moudleId, businessId, true);
        }

        public Param(int moudleId, long businessId, Post reply) {
            this(moudleId, businessId);
            this.posts = reply;
        }
    }

    public interface Listener {
        /**
         * 点击点赞区域
         */
        void onPraiseAreaClick();

        /**
         * 点赞成功后，点赞动画开始执行
         */
        void onPraiseSuccessAnimateionStart();

        /**
         * 点赞成功后，点赞动画结束
         */
        void onPraiseSuccessAnimationEnd();
    }
}
